import { bbs as BbsApi, user as UserApi } from '../../../../../config/request.js'
import {
    resources
} from '../../../../../util/';
module.exports = {
    name: 'bbs',
    data() {
        return {
            bbsDetailVisible: false,
            previewData: {
                bbs: {},
                user: {}
            },
            tableData: [],
            currentPage: 1,
            tableTotal: 10,
            loading: false,
            tableCount: resources.tableCount,
            layout: resources.layout,
            title: {
                info: {
                    prop: 'title',
                    label: '论坛标题',
                    sortable: true
                },
                filter: {
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            group: {
                info: {
                    prop: 'group',
                    label: '论坛模块',
                    sortable: true
                },
                filter: {
                    list: resources.bbs_groups,
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            status: {
                info: {
                    prop: 'status',
                    label: '论坛状态',
                    sortable: true
                },
                filter: {
                    list: resources.bbs_status,
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            content: {
                info: {
                    prop: 'content',
                    label: '内容缩略',
                    sortable: false
                },
                filter: {
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            like_count: {
                info: {
                    prop: 'like_count',
                    label: '点赞数',
                    sortable: true
                },
                filter: {
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            read_count: {
                info: {
                    prop: 'read_count',
                    label: '阅读数',
                    sortable: true
                },
                filter: {
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            comment_count: {
                info: {
                    prop: 'comment_count',
                    label: '评论数',
                    sortable: true
                },
                filter: {
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
            ctime: {
                info: {
                    prop: 'ctime',
                    label: '创建时间',
                    sortable: true
                },
                filter: {
                    multiple: false
                },
                style: {
                    width: '120',
                    align: 'center'
                }
            },
        }
    },
    methods: {
        formatterGroup(item) {
            return resources.bbs.formatterGroup(item.group);
        },
        formatterStatus(item) {
            return resources.bbs.formatterStatus(item.status);
        },
        formatterTime(item) {
            return resources.bbs.formatterTime(item.ctime);
        },
        formatterContent(item) {
            return resources.bbs.formatterContent(item.content);
        },
        filterGroup(value, item) {
            return item.group == value;
        },
        filterStatus(value, item) {
            return item.status == value;
        },
        /**
         * 校验权限 是否有管理员权限
         */
        checkLevel() {
            return resources.checkLevel(this);
        },
        /**
         * 页码改变的事件
         * @param {any} currentPage 跳转后页码
         */
        handleCurrentChange(currentPage) {
            this.currentPage = currentPage;
            this._getBbs();
        },
        //状态0：未定义 1：普通 2：置顶 3：精华 4：推荐
        onTop(bbs) {
            this.$confirm('是否确认把该贴状态设置为"置顶"？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._topBbs(bbs); //2
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onAddDigest(bbs) {
            this.$confirm('是否确认把该贴状态设置为"精华"？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._addDigestBbs(bbs); //3
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onRecommended(bbs) {
            this.$confirm('是否确认把该贴状态设置为"推荐"？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._recommendedBbs(bbs); //4
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onCancel(bbs) {
            this.$confirm('是否确认清除该贴的状态设置？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._cancelBbs(bbs); //1
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onDelete(bbs) {
            this.$confirm('是否确认删除该帖子？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this._deleteBbs(bbs); //0
            }).catch((err) => {
                this.$message.error(err);
            });
        },
        onPreview(bbs) {
            UserApi.findUser.call(this, { uid: bbs.uid }, data => {
                if (bbs.images) {
                    try {
                        bbs.images = JSON.parse(bbs.images);
                    } catch (err) {
                        bbs.images = '';
                    }
                }
                this.previewData.bbs = bbs;
                this.previewData.user = data['user'];
                this.bbsDetailVisible = true;
            })
        },
        _topBbs(bbs) {
            BbsApi.topBbs.call(this, { bid: bbs.bid }, data => {
                for (var i = this.tableData.length - 1; i >= 0; i--) {
                    if (this.tableData[i].bid == data['bbs'].bid) {
                        this.tableData[i].status = data['bbs'].status;
                        this.$message.success("设置成功");
                        return;
                    }
                }
            });
        }, //2
        _addDigestBbs(bbs) {
            BbsApi.addDigesBbs.call(this, { bid: bbs.bid }, data => {
                for (var i = this.tableData.length - 1; i >= 0; i--) {
                    if (this.tableData[i].bid == data['bbs'].bid) {
                        this.tableData[i].status = data['bbs'].status;
                        this.$message.success("设置成功");
                        return;
                    }
                }
            });
        }, //3
        _recommendedBbs(bbs) {
            BbsApi.recommendedBbs.call(this, { bid: bbs.bid }, data => {
                for (var i = this.tableData.length - 1; i >= 0; i--) {
                    if (this.tableData[i].bid == data['bbs'].bid) {
                        this.tableData[i].status = data['bbs'].status;
                        this.$message.success("设置成功");
                        return;
                    }
                }
            });
        }, //4
        _cancelBbs(bbs) {
            BbsApi.cancelBbs.call(this, { bid: bbs.bid }, data => {
                for (var i = this.tableData.length - 1; i >= 0; i--) {
                    if (this.tableData[i].bid == data['bbs'].bid) {
                        this.tableData[i].status = data['bbs'].status;
                        this.$message.success("设置成功");
                        return;
                    }
                }
            });
        }, //1
        _deleteBbs(bbs) {
            BbsApi.deleteBbs.call(this, { bid: bbs.bid }, data => {
                var index = this.tableData.indexOf(bbs);
                this.tableData.splice(index, 1);
                this.$message.success("删除成功");
            });
        }, //0
        _getBbs() {
            BbsApi.getBbs.call(this, { page: this.currentPage }, data => {
                this.tableData = data['lit'];
                this.tableTotal = data['count'];
            })
        }
    },
    created: function() {
        this._getBbs();
    }
}